package com.carrent.demo.Controller;

import com.carrent.demo.Domain.EmployeeInformation;
import com.carrent.demo.Domain.Users;
import com.carrent.demo.Service.EmployeeInformationService;
import com.carrent.demo.Service.UsersService;
import com.carrent.demo.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeInformationController {

    @Autowired
    EmployeeInformationService employeeInformationService;

    @Autowired
    UsersService usersService;

    @PostMapping("/login")
    public R Login(HttpServletRequest htr,@RequestBody Map map)
    {
        String password = map.get("password").toString();
        String account = map.get("username").toString();
        String radio = map.get("radio").toString();
        if(radio.equals("2"))
        {
            final EmployeeInformation login = employeeInformationService.login(account);
            if(login==null)return  R.error("用户名 不存在");
            if(!login.getPassword().equals(password))return  R.error("密码错误");
            htr.getSession().setAttribute("employee",login.getEmpId());
            return  R.success(login);
        }
        else if (radio.equals("1"))
        {
            final Users login = usersService.login(account);
            if(login==null)return  R.error("用户名 不存在");
            if(!login.getPassword().equals(password))return  R.error("密码错误");
            //  LambdaQueryWrapper<EmployeeInformation>
            htr.getSession().setAttribute("users",login.getUserId());
            return  R.success(login);
        }
        return  R.success("");
    }

    @GetMapping("/page")
    public  R getPage(Integer page, Integer pageSize, String name)
    {
        final List<EmployeeInformation> page1 = employeeInformationService.getPage((page-1)*pageSize, pageSize, name);
        final Integer count = employeeInformationService.getCount(name);
        return R.success(page1,count);
    }

    @PostMapping("/logout")
    public  R logout(HttpServletRequest htr)
    {
        htr.getSession().removeAttribute("employee");
        return  R.success("登出成功");
    }

    @PostMapping("/addEmployee")
    public  R addEmployee(@RequestBody EmployeeInformation employeeInformation)
    {
        final Boolean aBoolean = employeeInformationService.addEmployee(employeeInformation);
        return  R.success(aBoolean);
    }
    @DeleteMapping
    public  R deleteUser(@RequestParam(value = "ids") List<String> ids)
    {
        for(String s : ids)
        {
            employeeInformationService.deleteEmployee(s);

        }
        return R.success("");
    }

    @PutMapping
    public  R editUser(@RequestBody EmployeeInformation employeeInformation)
    {
        employeeInformationService.editEmployee(employeeInformation);
        return  R.success(1);
    }

}
